package com.zmx.patients.adapter;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.wang.adapters.adapter.BaseAdapterRvList;
import com.wang.adapters.base.BaseViewHolder;
import com.zmx.patients.R;
import com.zmx.patients.pojo.IntelligentChat;
import com.zmx.patients.view.activity.DiseaseIntroducedActivity;
import com.zmx.patients.view.util.MySharedPreferences;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/9/4  13:51
 * 功 能：
 */

public class IntelligentChatAdapter extends BaseAdapterRvList<IntelligentChatAdapter.ViewHolder, IntelligentChat> {

    private Handler handler;


    public IntelligentChatAdapter(Activity activity,Handler handler) {
        super(activity);
        this.handler = handler;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    protected void onBindVH(ViewHolder holder, int listPosition, IntelligentChat intelligentChat) {

        runEnterAnimation(holder.itemView,listPosition);

        if (intelligentChat.getGoods() == 1 && intelligentChat.getArticle() == 1){
            holder.linear.setVisibility(View.VISIBLE);

            holder.name_a.setText(intelligentChat.getKeyword()+"用什么药？");
            holder.name_b.setText(intelligentChat.getKeyword()+"的原因");
            holder.text_a.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent();
                    intent.putExtra("name",intelligentChat.getKeyword());
                    intent.putExtra("logo",3);
                    intent.setClass(mActivity, DiseaseIntroducedActivity.class);
                    mActivity.startActivity(intent);
                }

            });
            holder.text_b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent();
                    intent.putExtra("name",intelligentChat.getKeyword());
                    intent.putExtra("logo",4);
                    intent.setClass(mActivity, DiseaseIntroducedActivity.class);
                    mActivity.startActivity(intent);
                }

            });
        }else{
            holder.linear.setVisibility(View.GONE);
        }

        if (intelligentChat.getLogo() == 1){

            holder.left_msg.setVisibility(View.GONE);
            holder.results_layout.setVisibility(View.GONE);
            holder.right_msg.setVisibility(View.VISIBLE);
            holder.right_chat_data.setText(intelligentChat.getComtext());
            Glide.with(mActivity).load(MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.avatar,"")).into(holder.right_chat_head_icon);

        }else if(intelligentChat.getLogo() == 2){

            holder.left_msg.setVisibility(View.GONE);
            holder.right_msg.setVisibility(View.GONE);
            holder.results_layout.setVisibility(View.VISIBLE);
            holder.store_message.setText("共有"+intelligentChat.getNumber()+"种可能的疾病供您参考");
            holder.results_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Message message = handler.obtainMessage();
                    message.what = 5;
                    handler.sendMessage(message);


                }
            });


        }else{

            holder.left_msg.setVisibility(View.VISIBLE);
            holder.right_msg.setVisibility(View.GONE);
            holder.results_layout.setVisibility(View.GONE);

            String duo = "";
            if (intelligentChat.getType() == 1){

                duo = "（多选）";

            }else{

                duo="";

            }

            holder.left_chat_title.setText(intelligentChat.getTitle()+duo);

            if (intelligentChat.getComtext() == null){

                holder.left_chat_context.setVisibility(View.GONE);

            }else{

                holder.left_chat_context.setText(intelligentChat.getComtext());

            }


            if (intelligentChat.getHint_message() == null || intelligentChat.getHint_message().equals("")){

                holder.hint_message.setVisibility(View.GONE);

            }else{

                holder.hint_message.setVisibility(View.VISIBLE);
                holder.hint_message.setText(intelligentChat.getHint_message());

            }


        }

    }


    @NonNull
    @Override
    protected ViewHolder onCreateVH(ViewGroup parent, LayoutInflater inflater) {
        IntelligentChatAdapter.ViewHolder holder = new IntelligentChatAdapter.ViewHolder(inflater.inflate(R.layout.adapter_intelligent_chat, parent, false));
        return holder;
    }

    static class ViewHolder extends BaseViewHolder {

        @BindView(R.id.right_chat_head_icon)
        CircleImageView right_chat_head_icon;
        @BindView(R.id.left_msg)
        RelativeLayout left_msg;
        @BindView(R.id.right_msg)
        RelativeLayout right_msg;
        @BindView(R.id.left_chat_title)
        TextView left_chat_title;
        @BindView(R.id.hint_message)
        TextView hint_message;
        @BindView(R.id.name_a)
        TextView name_a;
        @BindView(R.id.name_b)
        TextView name_b;
        @BindView(R.id.text_a)
        TextView text_a;
        @BindView(R.id.text_b)
        TextView text_b;
        @BindView(R.id.left_chat_context)
        TextView left_chat_context;
        @BindView(R.id.right_chat_data)
        TextView right_chat_data;
        @BindView(R.id.store_message)
        TextView store_message;
        @BindView(R.id.results_layout)
        LinearLayout results_layout;
        @BindView(R.id.linear)
        LinearLayout linear;
        @BindView(R.id.results_button)
        Button results_button;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

    }

    private int lastAnimatedPosition=-1;
    private boolean animationsLocked = false;
    private boolean delayEnterAnimation = true;
    private int itemCount=0;
    private void runEnterAnimation(View view, int position) {

        if (animationsLocked) return;              //animationsLocked是布尔类型变量，一开始为false
        //确保仅屏幕一开始能够容纳显示的item项才开启动画



        if (position > lastAnimatedPosition) {//lastAnimatedPosition是int类型变量，默认-1，
            //这两行代码确保了recyclerview滚动式回收利用视图时不会出现不连续效果
            lastAnimatedPosition = position;
            view.setTranslationY(500);     //Item项一开始相对于原始位置下方500距离
            view.setAlpha(0.f);           //item项一开始完全透明
            //每个item项两个动画，从透明到不透明，从下方移动到原始位置


            view.animate()
                    .translationY(0).alpha(1.f)                                //设置最终效果为完全不透明
                    //并且在原来的位置
                    .setStartDelay(delayEnterAnimation ? 20 * (position) : 0)//根据item的位置设置延迟时间
                    //达到依次动画一个接一个进行的效果
                    .setInterpolator(new DecelerateInterpolator(0.5f))     //设置动画位移先快后慢的效果
                    .setDuration(700)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            animationsLocked = true;
                            //确保仅屏幕一开始能够显示的item项才开启动画
                            //也就是说屏幕下方还没有显示的item项滑动时是没有动画效果
                        }
                    })
                    .start();
        }
    }


}
